package com.atguigu.dga.governance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.mapper.GovernanceAssessGlobalMapper;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 治理总考评表 服务实现类
 * </p>
 *
 * @author wuzhida
 * @since 2024-07-02
 */
@Service
@DS("dga")
public class GovernanceAssessGlobalServiceImpl extends ServiceImpl<GovernanceAssessGlobalMapper, GovernanceAssessGlobal> implements GovernanceAssessGlobalService {

    /**
     * 计算全局分数
     *
     * @param assessDate
     */
    @Override
    public void calcGlobalScore(String assessDate) {
        remove(
                new QueryWrapper<GovernanceAssessGlobal>()
                        .eq("assess_date", assessDate)
        );
        List<GovernanceAssessGlobal> globalScoreList = getBaseMapper().getGlobalScore(assessDate);
        saveBatch(globalScoreList);
    }

    /**
     * 全局分数
     * 返回值{"assessDate":"2023-04-01" ,"sumScore":90, "scoreList":[20,40,34,55,66]}
     * @return
     */
    @Override
    public String globalScore() {
        GovernanceAssessGlobal assessGlobal = getOne(
                new QueryWrapper<GovernanceAssessGlobal>()
                        .inSql("assess_date", "SELECT MAX(assess_date) FROM governance_assess_global")
        );
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate", assessGlobal.getAssessDate());
        jsonObject.put("sumScore", assessGlobal.getScore());
        List<BigDecimal> scoreList = Arrays.asList(
                assessGlobal.getScoreSpec().multiply(BigDecimal.TEN), 
                assessGlobal.getScoreStorage().multiply(BigDecimal.TEN), 
                assessGlobal.getScoreCalc().multiply(BigDecimal.TEN), 
                assessGlobal.getScoreQuality().multiply(BigDecimal.TEN),
                assessGlobal.getScoreSecurity().multiply(BigDecimal.TEN)
        );
        jsonObject.put("scoreList", scoreList);
        return jsonObject.toJSONString();
    }
}
